package Member;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;

public class Input extends JPanel {
	
	Data6[] d;
	
	private JLabel lblName;
	private JTextField nameInput;
	private JLabel lblAddress;
	private JTextField addressInput;
	private JLabel lblPhone;
	private JTextField phoneInput;
	private JLabel lblEmail;
	private JTextField emailInput;
	private JLabel lblAge;
	private JTextField ageInput;
	private JButton btnInput;
	private JLabel lblMemberManageSystem;
	private JLabel status;

	/**
	 * Create the panel.
	 */
	
	public void sleep(int time) {
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
		}
	}
	
	public Input() {
		setSize(new Dimension(450, 500));
		setBorder(new LineBorder(new Color(139, 0, 0)));
		setBackground(new Color(250, 128, 114));
		setLayout(null);
		add(getLblName());
		add(getNameInput());
		add(getLblAddress());
		add(getAddressInput());
		add(getLblPhone());
		add(getPhoneInput());
		add(getLblEmail());
		add(getEmailInput());
		add(getLblAge());
		add(getAgeInput());
		add(getBtnInput());
		add(getLabel_1());
		add(getStatus());

	}
	private JLabel getLblName() {
		if (lblName == null) {
			lblName = new JLabel("Name");
			lblName.setBounds(120, 82, 65, 23);
		}
		return lblName;		
	}
	private JTextField getNameInput() {
		if (nameInput == null) {
			nameInput = new JTextField();
			nameInput.setBounds(197, 82, 116, 23);
			nameInput.setColumns(10);
		}
		return nameInput;
	}
	private JLabel getLblAddress() {
		if (lblAddress == null) {
			lblAddress = new JLabel("Address");
			lblAddress.setBounds(120, 126, 65, 23);
		}
		return lblAddress;
	}
	private JTextField getAddressInput() {
		if (addressInput == null) {
			addressInput = new JTextField();
			addressInput.setColumns(10);
			addressInput.setBounds(197, 126, 116, 23);
		}
		return addressInput;
	}
	private JLabel getLblPhone() {
		if (lblPhone == null) {
			lblPhone = new JLabel("Phone");
			lblPhone.setBounds(120, 170, 65, 23);
		}
		return lblPhone;
	}
	private JTextField getPhoneInput() {
		if (phoneInput == null) {
			phoneInput = new JTextField();
			phoneInput.setColumns(10);
			phoneInput.setBounds(197, 170, 116, 23);
		}
		return phoneInput;
	}
	private JLabel getLblEmail() {
		if (lblEmail == null) {
			lblEmail = new JLabel("Email");
			lblEmail.setBounds(120, 214, 65, 23);
		}
		return lblEmail;
	}
	private JTextField getEmailInput() {
		if (emailInput == null) {
			emailInput = new JTextField();
			emailInput.setColumns(10);
			emailInput.setBounds(197, 214, 116, 23);
		}
		return emailInput;
	}
	private JLabel getLblAge() {
		if (lblAge == null) {
			lblAge = new JLabel("Age");
			lblAge.setBounds(120, 258, 65, 23);
		}
		return lblAge;
	}
	private JTextField getAgeInput() {
		if (ageInput == null) {
			ageInput = new JTextField();
			ageInput.setColumns(10);
			ageInput.setBounds(197, 258, 116, 23);
		}
		return ageInput;
	}
	private JButton getBtnInput() {
		if (btnInput == null) {
			btnInput = new JButton("Input");
			btnInput.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(d.length==10){
						status.setText("Members are fulled");
					}					
					
					//데이터 입력 여부 체크
					//데이터가 한개라도 입력이 안되면 입력이 안되도록 설정
					boolean flag = true; // 모든 값을 입력했을때 
					if(nameInput.getText().equals("")) flag=false;
					else if(addressInput.getText().equals("")) flag=false;
					else if(phoneInput.getText().equals("")) flag=false;
					else if(emailInput.getText().equals("")) flag=false;
					else if(ageInput.getText().equals("")) flag=false;
					try {
						Integer.parseInt(ageInput.getText());
					} catch (Exception e) {
						status.setText("Put a numbers in age");
						flag=false;					
					}				
					
					if(flag){
						Data6 data = new Data6();
						data.name = nameInput.getText();
						data.address = addressInput.getText();
						data.number = phoneInput.getText();
						data.email = emailInput.getText();
						data.age = Integer.parseInt(ageInput.getText());
						
						for (int pos=0; pos<d.length;pos++){
							if(d[pos] == null){
								
								d[pos] = data;
								
								//입력 종료후 모든 칸을 공백으로
								nameInput.setText(""); 
								addressInput.setText("");
								phoneInput.setText("");
								emailInput.setText("");
								ageInput.setText("");
								
								//커서를 NameInput 칸으로 이동
								nameInput.requestFocus();
								
								status.setText("Update successful");
								
								status.setText("                Please Input Your Data");
								
								break;
							}
						}
					}					
				}
			});
			btnInput.setBounds(197, 300, 116, 23);
		}
		return btnInput;
	}
	private JLabel getLabel_1() {
		if (lblMemberManageSystem == null) {
			lblMemberManageSystem = new JLabel("Member Input");
			lblMemberManageSystem.setFont(new Font("Segoe Print", Font.BOLD, 16));
			lblMemberManageSystem.setBounds(120, 27, 200, 50);
		}
		return lblMemberManageSystem;
	}
	
	public void setD(Data6[] a){
		this.d=a;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("                Please Input Your Data");
			status.setBackground(new Color(255, 218, 185));
			status.setOpaque(true);
			status.setBounds(120, 334, 193, 15);
		}
		return status;
	}
	
}
